﻿using Lib.Data.Enumeration;
using System.Data.Common;

namespace Lib.Data.Entitys;
/// <summary>
/// 数据库连接 ConnectionStrings 配置实体类
/// </summary>
[Serializable]
public partial class Connection {
    /// <summary>
    /// 构造函数
    /// </summary>
    public Connection() {
        ConnectionStrings = []; 
        Providers = [];
        SQLs = [];
        ConnectiveStrings = [];
    }
    /// <summary>
    /// 
    /// </summary>
    public void Initialize() {
        //
        for (var i = 0; i < ConnectionStrings.Count; i++) {
            var e = ConnectionStrings.ElementAt(i);
            if (string.IsNullOrEmpty(e.Key) || e.Value == null) {
                ConnectionStrings.Remove(e.Key);
                i--;
            } else {
                var P = e.Value;
                if (string.IsNullOrEmpty(P.Key)) {
                    P.Key = e.Key;
                }
                P.ConnectType = ConnectType.Key;
                if (string.IsNullOrEmpty(P.Provider)) {
                    P.Provider = "MSSQL";
                    P.dbType = dbType.MSSQL;
                } else {
                    if (Enum.TryParse<dbType>(P.Provider, true, out P.dbType)) {

                    } else {
                        P.dbType = dbType.UnClaim;
                    }
                }
            }
        }
        //
        for (var i = 0; i < Providers.Count; i++) {
            var e = Providers.ElementAt(i);
            if (string.IsNullOrEmpty(e.Key) || string.IsNullOrEmpty(e.Value)) {
                Providers.Remove(e.Key);
                i--;
            } else {
                if (e.Value.StartsWith("Microsoft.Data.", StringComparison.OrdinalIgnoreCase) || e.Value.StartsWith("System.Data.", StringComparison.OrdinalIgnoreCase)) {
                    DbProviderFactories.RegisterFactory(e.Key, e.Value);
                } else {
                    var Results = e.Value.Split(',');
                    if(Results.Length > 1) {
                        var File = System.IO.Path.Combine("Providers", e.Key, $"{Results[1].Trim()}.DLL").Replace(@"\", "/");
                        var ProviderFactoryClass = Results[0].Trim();
                        var ProviderFactoryClassType = AssemblyLoader.LoadAssemblyFile(File, ProviderFactoryClass);
                        if (ProviderFactoryClassType != null) {
                            DbProviderFactories.RegisterFactory(e.Key, ProviderFactoryClassType);
                        }
                    }
                }
            }
        }
        //
        for (var i = 0; i < SQLs.Count; i++) {
            var e = SQLs.ElementAt(i);
            if (string.IsNullOrEmpty(e.Key) || e.Value == null) {
                SQLs.Remove(e.Key);
                i--;
            } else {
                var P = e.Value;
                if (Enum.TryParse<dbType>(e.Key, true, out P.dbType)) {

                } else {
                    P.dbType = dbType.UnClaim;
                }
                for (var ii = 0; ii < P.SQLs.Count; ii++) {
                    var SQL = P.SQLs.ElementAt(ii);
                    if (string.IsNullOrEmpty(SQL.Key) || string.IsNullOrEmpty(SQL.Value)) {
                        P.SQLs.Remove(SQL.Key); 
                        ii--;
                    }
                }
                if (string.IsNullOrEmpty(P.Key) || !P.SQLs.ContainsKey(P.Key)) {
                    if (P.SQLs.Count > 0) {
                        P.Key = P.SQLs.ElementAt(0).Key;
                    } else {
                        P.Key = null;
                    }
                }
            }
        }
        //
        for (var i = 0; i < ConnectiveStrings.Count; i++) {
            var e = ConnectiveStrings.ElementAt(i);
            if (string.IsNullOrEmpty(e.Key) || e.Value == null || e.Value.Count == 0) {
                ConnectiveStrings.Remove(e.Key); i--;
            } else {
                for (var ii = 0; ii < e.Value.Count; ii++) {
                    var P = e.Value.ElementAt(ii);
                    if (string.IsNullOrEmpty(P.Key) || string.IsNullOrEmpty(P.Value)) {
                        e.Value.Remove(P.Key);
                        ii--;
                    } 
                }
            }
        }
    }
    /// <summary>
    /// 默认数据库连接关键字
    /// </summary>
    public string ConnectKey { get; set; }
    /// <summary>
    /// 数据库连接集合 Key = 数据库连接关键字 = Connect.Key
    /// </summary>
    public Dictionary<string, Connect> ConnectionStrings { get; set; }
    /// <summary>
    /// 数据库提供程序集合 Key = 提供程序关键字（默认是数据类型）
    /// </summary>
    public Dictionary<string, string> Providers { get; set; }
    /// <summary>
    /// 分页配置集合（ Key = 数据库类型关键字 ）
    /// </summary>
    public Dictionary<string, Pagination> SQLs { get; set; }
    /// <summary>
    /// 数据库连接提供程序驱动集合( Key = 数据库类型关键字，Value.Key = 数据库提供程序关键字
    /// </summary>
    public Dictionary<string, Dictionary<string, string>> ConnectiveStrings { get; set; }

}

